package www13;

// 定义一个接口或抽象类，表示组件
interface Component {
    void operation();
}

// 具体的组件类
class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("具体组件操作");
    }
}

// 装饰者抽象类
abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}

// 具体的装饰者类A
class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addedBehavior();
    }

    public void addedBehavior() {
        System.out.println("装饰者A增加的行为");
    }
}

// 具体的装饰者类B
class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addedBehavior();
    }

    public void addedBehavior() {
        System.out.println("装饰者B增加的行为");
    }
}

// 测试装饰者模式
public class Main {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();

        // 装饰者A
        Component decoratorA = new ConcreteDecoratorA(component);

        // 装饰者B
        Component decoratorB = new ConcreteDecoratorB(decoratorA);

        // 执行操作
        decoratorB.operation();
    }
}